package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;


    /**
     * 微信支付
     * @param paymentType
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "微信下单")
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result createJsapi(@PathVariable String paymentType,
                              @PathVariable String orderNo){
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法 map;
        Map<String,Object> map = wxPayService.createJsapi(paymentType,orderNo,userId);
        //  返回数据
        return Result.ok(map);
    }

    /**
     * 查询支付状态
     * @param orderNo
     * @return
     */
    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo){
        //  调用服务层方法
        Transaction result = this.wxPayService.queryPayStatus(orderNo); // Transaction result = service.queryOrderById(queryRequest);
        //  判断
        if (null!=result && result.getTradeState()== Transaction.TradeStateEnum.SUCCESS){
            //  表示支付成功.
            paymentInfoService.updatePaymentStatus(result);
            return Result.ok(true);
        }
        //  返回数据
        return Result.ok(false);
    }

    /**
     * 微信异步回调
     * @return
     */
    @Operation(summary = "异步回调")
    @PostMapping("notify")
    public Result wxNotify(HttpServletRequest request){
        System.out.println("来人了，开始接客了....");
        //  创建一个map集合
        Map<String,Object> result = new HashMap<>();
        try {
            //  调用服务层方法.
            this.wxPayService.wxNotify(request);
            //  返回码
            result.put("code","SUCCESS");
            //  返回消息
            result.put("message","成功");
            //  返回数据集
            return Result.ok(request);
        } catch (Exception e) {
            //  获取异常信息。
            e.getMessage();
        }
        //  返回码
        result.put("code","FAIL");
        //  返回消息
        result.put("message","失败");
        //  返回数据
        return Result.ok(request);
    }
}
